#include "SimpleJobChooser.cuh"

SimpleJobChooser::SimpleJobChooser(vector <Clause> * formula, int number_of_variables,
			vector<Var> * dead_variables) :
			chosen_variables()
{
	this->number_of_variables = number_of_variables;
	this->formula = formula;
	this->dead_vars = dead_variables;
	this->evaluated = false;

}

SimpleJobChooser::~SimpleJobChooser()
{

}
void SimpleJobChooser::evaluate()
{
	int total_vars = number_of_variables - dead_vars->size();
	int vars_to_be_chosen = total_vars < UNIFORM_NUMBER_OF_VARS ?
			total_vars : UNIFORM_NUMBER_OF_VARS;

	int chosen_vars_number = 0;

	for(int i = 0; chosen_vars_number < vars_to_be_chosen; i++)
	{
		Var v = i;
		bool dead = false;

		for (std::vector<Var>::iterator it = dead_vars->begin();
				it != dead_vars->end(); ++it)
		{
			if(*it == v)
			{
				dead = true;
				break;
			}
		}
		if(!dead)
		{
			chosen_variables.push_back(v);
			chosen_vars_number++;
		}

		assert(i < number_of_variables);
	}

	variables_per_job = chosen_vars_number;
	evaluated = true;

}
void SimpleJobChooser::getJobs(JobsQueue & queue)
{
#ifdef USE_ASSERTIONS
	assert(evaluated);
#endif

	Lit* fixed_lits = (Lit*) malloc(sizeof(Lit)*variables_per_job);

	addJobs(fixed_lits, 0, queue);

}

void SimpleJobChooser::addJobs(Lit* fixed_lits, int number_fixed_lits, JobsQueue & queue)
{

	if(number_fixed_lits == variables_per_job)
	{
		Job j = mkJob_dev(fixed_lits, number_fixed_lits);
		queue.add(j);

		return;
	}

	Lit lit = mkLit(chosen_variables[number_fixed_lits], true);

	fixed_lits[number_fixed_lits] = lit;

	addJobs(fixed_lits, number_fixed_lits+1, queue);

	lit = mkLit(chosen_variables[number_fixed_lits], false);

	fixed_lits[number_fixed_lits] = lit;

	addJobs(fixed_lits, number_fixed_lits+1, queue);

}

int SimpleJobChooser::get_number_of_jobs()
{
	return (int) pow(2,(float)variables_per_job);
}
bool SimpleJobChooser::is_evaluated()
{
	return evaluated;
}
